package com.etogc.groupchat.group.adpt;

/**
 * Created by McGrady on 2017/6/11.
 */

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.etogc.groupchat.R;
import com.etogc.util.GcUrl;
import com.etogc.util.ImageUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 来访记录适配器
 */
public class MemberAdapter extends RecyclerView.Adapter<MemberAdapter.MyViewHolder> {

    private final Context mContext;

    private List<HashMap<String, String>> mList = new ArrayList<>();

    public MemberAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<HashMap<String, String>> list) {
        if (list != null) {
            this.mList = list;
            notifyDataSetChanged();
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_member, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        try {
            Map<String, String> map = mList.get(position);
            String nickname = String.valueOf(map.get("nickname"));
            String photo = String.valueOf(map.get("photo"));
//            String id = String.valueOf(map.get("id"));

            if (photo != null && !TextUtils.isEmpty(photo)) {
                ImageUtils.setRadiuImage(mContext, ImageUtils.getImageUrl(photo), holder.ivHead);
            }

            if (!TextUtils.isEmpty(nickname)) {
                holder.tvName.setText(String.valueOf(nickname));
            } else {
                holder.tvName.setText("无名");
            }

//            holder.tvUserId.setText("用户ID: " + id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView ivHead;
        TextView tvName, tvUserId;
        View item;

        public MyViewHolder(View itemView) {
            super(itemView);
            ivHead = itemView.findViewById(R.id.ivHead);
            tvName = itemView.findViewById(R.id.tvName);
            setTextMarquee(tvName);
//            tvUserId = itemView.findViewById(R.id.tvUserId);
            item = itemView.findViewById(R.id.rootView);
        }
    }

    public static void setTextMarquee(TextView textView) {
        if (textView != null) {
            textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
            textView.setSingleLine(true);
            textView.setSelected(true);
            textView.setFocusable(true);
            textView.setFocusableInTouchMode(true);
        }
    }
}